package com.example.jack.community;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import static android.content.Context.ACTIVITY_SERVICE;
import static it.sephiroth.android.library.imagezoom.ImageViewTouchBase.LOG_TAG;

/**
 * Created by ${justin} on 2017/10/2112: 05
 * WeChat:Justin-Tz
 */

public class BaseFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_base, container, false);
    }
    //查看内存剩余判断是否内存过低
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private String getCurrentMeminfo() {
        StringBuffer sb = new StringBuffer();
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        ActivityManager activityManager = (ActivityManager) getActivity().getSystemService(ACTIVITY_SERVICE);
        activityManager.getMemoryInfo(mi);
        sb.append("剩余内存："+(mi.availMem/1024/1024)+"MB\n");//当前手机剩余的内存
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            sb.append("总内存： " + (mi.totalMem/1024/1024) + "MB\n"); //当前手机可用的总内存
        }
        sb.append("内存是否过低：" + mi.lowMemory);
        return sb.toString();
    }
    /**
     * 清理内存
     */
    public void clearMem() {
        ActivityManager activityManger = (ActivityManager) getContext().getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appList = activityManger.getRunningAppProcesses();
        if (appList != null) {
            for (int i = 0; i < appList.size(); i++) {
                ActivityManager.RunningAppProcessInfo appInfo = appList.get(i);

                Log.v(LOG_TAG, "pid: " + appInfo.pid);
                Log.v(LOG_TAG, "processName: " + appInfo.processName);
                Log.v(LOG_TAG, "importance: " + appInfo.importance);

                String[] pkgList = appInfo.pkgList;
                if (appInfo.importance > ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                    for (int j = 0; j < pkgList.length; j++) {
                        activityManger.killBackgroundProcesses(pkgList[j]);
                    }
                }
            }
        }
    }
}
